home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c2 / tmultbox.cpp < prev    next >
Text File  |  1992-11-12  |  3KB  |  134 lines

  1. /***************************************************************************
  2. TMultiple Box:
  3.     written by Michael Ogrinz       70007,6544
  4.     September,1992
  5.  
  6. Usage:  Simply use this in a dialog exactly as you would a TListBox. When
  7.     you assign the collection (with the newList method) make sure that
  8.     the items in the list start with a blank space so that the
  9.     marked tag will not erase the first character of the list item.
  10.  
  11.     Items can be marked either by pressing Enter, or clicking on
  12.     them with the left mouse button. The following hot-keys are
  13.     also defined:
  14.     
  15.     kbF7: Mark all items.
  16.     kbF8: Unmark all items.
  17.     kbF9: Toggle Marked/Unmarked items.
  18.  
  19.     Please inform me of any comments, concerns, etc. This is the first
  20.     piece of code I've ever u/l'd to CIS!
  21. ****************************************************************************/
  22.  
  23.  
  24. class TMultipleBox:public TListBox
  25. {
  26.    private:
  27.     void ToggleItem(int item_no);
  28.     void MarkAll();
  29.     void UnMarkAll();
  30.     void ToggleAll();
  31.    public:
  32.     //simply calls base class constructor for now:
  33.     TMultipleBox(const TRect& bounds, ushort aNumCols,
  34.              TScrollBar *aScrollBar);
  35.     char tag;
  36.     virtual void handleEvent(TEvent& event);
  37. };
  38.  
  39. TMultipleBox::TMultipleBox(const TRect& bounds, ushort aNumCols,
  40.                TScrollBar *aScrollBar)
  41.          :TListBox(bounds,aNumCols,aScrollBar)
  42. {
  43.     // calls base class constructor; sets tag character.
  44.     tag='';
  45. }
  46.  
  47. void TMultipleBox::handleEvent(TEvent& event)
  48. {
  49.     // we dont want to lose mouse button events to the main handler....
  50.     if ((event.what==evMouseDown) && (event.mouse.buttons==mbLeftButton))
  51.     {
  52.         TListBox::handleEvent(event);
  53.         ToggleItem(focused);
  54.         TListBox::draw();
  55.     }
  56.     else
  57.         TListBox::handleEvent(event);
  58.  
  59.     switch (event.what)
  60.     {
  61.         case evKeyDown:
  62.             switch(event.keyDown.keyCode)
  63.             {
  64.                 case kbEnter:
  65.                     ToggleItem(focused);
  66.                     break;
  67.                 case kbF7:
  68.                     MarkAll();
  69.                     break;
  70.                 case kbF8:
  71.                     UnMarkAll();
  72.                     break;
  73.                 case kbF9:
  74.                     ToggleAll();
  75.                     break;
  76.             }
  77.             TListBox::draw();
  78.             break;
  79.     }
  80. }
  81.  
  82. void TMultipleBox::ToggleItem(int item_no)
  83. {
  84.     if (range<=0)
  85.         return;
  86.  
  87.     char *itemaddr;
  88.     itemaddr=(char *)(list()->at(item_no));
  89.     if (itemaddr[0]==tag)
  90.         itemaddr[0]=' ';
  91.     else
  92.         itemaddr[0]=tag;
  93. }
  94.  
  95. void TMultipleBox::MarkAll()
  96. {
  97.     if (range<=0)
  98.         return;
  99.     char *itemaddr;
  100.     for (int i=0;i<range;i++)
  101.     {
  102.         itemaddr=(char *)(list()->at(i));
  103.         itemaddr[0]=tag;
  104.     }
  105. }
  106.  
  107. void TMultipleBox::UnMarkAll()
  108. {
  109.     if (range<=0)
  110.         return;
  111.     char *itemaddr;
  112.     for (int i=0;i<range;i++)
  113.     {
  114.         itemaddr=(char *)(list()->at(i));
  115.         itemaddr[0]=' ';
  116.     }
  117. }
  118.  
  119. void TMultipleBox::ToggleAll()
  120. {
  121.     if (range<=0)
  122.         return;
  123.     char *itemaddr;
  124.     for (int i=0;i<range;i++)
  125.     {
  126.         itemaddr=(char *)(list()->at(i));
  127.         if (itemaddr[0]==tag)
  128.             itemaddr[0]=' ';
  129.         else
  130.             itemaddr[0]=tag;
  131.     }
  132. }
  133. /***************************************************************************/
  134.